package com.se.hospitalmonitoring;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import com.se.hospitalmonitoring.VitalSign.VitalSignMeasurement;

public class PatientHistoryInterface extends JFrame {

	private static final long serialVersionUID = 1L;
	private final JPanel contentPane;
	public static final long ALL_HISTORY = -1L;

	/**
	 * Launch the GUI only.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PatientHistoryInterface frame = new PatientHistoryInterface(
						new PatientRecord(1, "Sam", 21), -1, -1);
					frame.setVisible(true);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PatientHistoryInterface(PatientRecord patient, long start, long end) {
		boolean showAllHistory = start == ALL_HISTORY;

		setTitle(patient.name);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 800, 600);

		contentPane = new JPanel();
		JScrollPane scrollFrame = new JScrollPane(contentPane);
		contentPane.setAutoscrolls(true);
		scrollFrame.setPreferredSize(new Dimension(750, 300));

		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(scrollFrame);
		Set<VitalSign> vitalSigns = patient.history.getVitalSigns();
		contentPane.setLayout(new GridLayout(vitalSigns.size(), 1, 0, 10));
		Iterator<VitalSign> itr = vitalSigns.iterator();
		while (itr.hasNext()) {
			VitalSign vs = itr.next();
			TimeSeriesCollection dataset = new TimeSeriesCollection();
			TimeSeries series = new TimeSeries(vs.name);
			dataset.addSeries(series);

			ArrayList<VitalSignMeasurement> vsHist = patient.history
				.getHistory(vs);
			for (VitalSignMeasurement vsm : vsHist) {
				if (!showAllHistory && vsm.timestamp.getTime() > end)
					break;
				if (showAllHistory || vsm.timestamp.getTime() > start)
					series.add(new Second(vsm.timestamp), vsm.getValue());
			}

			JFreeChart chart = ChartFactory.createTimeSeriesChart(vs.name,
				"Time", vs.name + "(" + vs.unitName + ")", dataset, false,
				true, false);
			chart.setBackgroundPaint(Color.LIGHT_GRAY);
			final XYPlot plot = chart.getXYPlot();
			ValueAxis domain = plot.getDomainAxis();
			domain.setAutoRange(true);
			ValueAxis range = plot.getRangeAxis();
			range.setRange(0, 100 * vs.scaleFactor);
			ChartPanel chartPanel = new ChartPanel(chart);
			chartPanel.setBackground(Color.LIGHT_GRAY);
			contentPane.add(chartPanel);
		}
	}

}
